var app = getApp();
Page({
  data: {
    user: null
  },
  onLoad: function(options) {
    this.setData({
      statusBarHeight: app.globalData.systemInfo.statusBarHeight + 15,
    })
    var user = app.globalData.user;
    if (user.birthday != null) {
      var fullYear = parseInt(user.birthday.substring(0, 4));
      var month = parseInt(user.birthday.substring(5, 7));
      var day = parseInt(user.birthday.substring(8, 10));
      user.showBirthday = fullYear.toString() + '-' + month.toString() + '-' + day.toString();
    }
    this.setData({
      user: user
    })
  },
  //返回下一个页面
  rollback: function() {
    wx.navigateBack({
      delta: 1
    })
  },
  //更改常住地
  updateAddress: function(e) {
    var province = e.detail.value[0];
    var city = e.detail.value[1];
    var district = e.detail.value[2];
    this.setData({
      address: province + city + district
    })
    var data = {
      id: app.globalData.user.id,
      province: province,
      city: city,
      district: district
    }
    this.updateUserAddress(data);
  },

  //更改性别
  updateGender: function() {
    var that = this;
    wx.showActionSheet({
      itemList: ['女', '男'],
      success: function(e) {
        var gender = e.tapIndex;
        var data = {
          id: app.globalData.user.id,
          gender: gender
        }
        that.updateUserAddress(data);
      }
    })
  },
  //更改生日
  updateBrithday: function(e) {
    var birthday = e.detail.value;
    var data = {
      id: app.globalData.user.id,
      birthday: birthday + ' 00:00:00.000'
    }
    this.updateUserAddress(data);
  },

  //更改信息
  updateUserAddress: function(data) {
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'updateUserAddress',
      data: data,
      method: 'POST',
      success: function(r) {
        if (r.data.code == 200) {
          var user = r.data.data;
          app.globalData.user = user;
          if (user.birthday != null) {
            var fullYear = parseInt(user.birthday.substring(0, 4));
            var month = parseInt(user.birthday.substring(5, 7));
            var day = parseInt(user.birthday.substring(8, 10));
            user.showBirthday = fullYear.toString() + '-' + month.toString() + '-' + day.toString();
          }
          that.setData({
            user: user
          })
        } else {
          wx.showToast({
            title: '更改个人信息失败',
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: function() {
        wx.showToast({
          title: '网络断开连接,更改个人信息失败',
          icon: 'none',
          duration: 3000
        })
      }
    })
  }
})